import time
import pymysql
import configparser
from DBUtils.PooledDB import PooledDB

#数据库地址池
class Mysql(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            cls.__instance = super(Mysql, cls).__new__(cls, *args, **kwargs)
        return cls.__instance

    def __init__(self):
        self.config_file = r'conf\config.ini'
        self.config = configparser.ConfigParser()
        self.config.read(self.config_file, encoding="utf-8")
        _host = self.config.get('mysql', 'host')
        _dbname = self.config.get('mysql', 'dbname')
        _user = self.config.get('mysql', 'username')
        _pwd = self.config.get('mysql', 'passwd')
        self.mysql = PooledDB(creator=pymysql, mincached=10, maxcached=20,
                              host=_host, port=3306, user=_user, passwd=_pwd,
                              db=_dbname, charset="utf8")

    def getAll(self, sql):
        _conn = self.mysql.connection()
        _cursor = _conn.cursor()
        _cursor.execute(sql)
        result = _cursor.fetchall()
        _cursor.close()
        _conn.close()
        return result

    def getOne(self, sql):
        _conn = self.mysql.connection()
        _cursor = _conn.cursor()
        _cursor.execute(sql)
        result = _cursor.fetchone()
        _cursor.close()
        _conn.close()
        return result

class InfoMysql(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            cls.__instance = super(InfoMysql, cls).__new__(cls, *args, **kwargs)
        return cls.__instance

    def __init__(self):
        self.config_file = r'conf\config.ini'
        self.config = configparser.ConfigParser()
        self.config.read(self.config_file, encoding="utf-8")
        _host = self.config.get('mysql', 'host')
        _dbname = self.config.get('mysql', 'dbname_info')
        _user = self.config.get('mysql', 'username')
        _pwd = self.config.get('mysql', 'passwd')
        self.mysql = PooledDB(creator=pymysql, mincached=10, maxcached=20,
                              host=_host, port=3306, user=_user, passwd=_pwd,
                              db=_dbname, charset="utf8")

    def getAll(self, sql):
        _conn = self.mysql.connection()
        _cursor = _conn.cursor()
        _cursor.execute(sql)
        result = _cursor.fetchall()
        _cursor.close()
        _conn.close()
        return result

    def getOne(self, sql):
        _conn = self.mysql.connection()
        _cursor = _conn.cursor()
        _cursor.execute(sql)
        result = _cursor.fetchone()
        _cursor.close()
        _conn.close()
        return result


#定义装饰器函数，功能是传进来的函数进行包装并返回包装后的函数
def performance(f):
    def fn(*args, **kw):                                            #对传进来的函数进行包装的函数
        t_start = time.time()                                       #记录函数开始时间
        r = f(*args, **kw)                                          #调用函数
        t_end = time.time()                                         #记录函数结束时间
        print('函数：%s() 执行时长：%fs' % (f.__name__, (t_end - t_start)))  #打印调用函数的属性信息，并打印调用函数所用的时间
        return r                                                    #返回包装后的函数
    return fn                                                      #调用包装后的函数



#